<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="活动标题">
        <el-input v-model="addData.name" placeholder="请输入活动标题"  style="width:200px" />
      </el-form-item>
      <!-- 讲师头像：TODO -->
      <el-form-item label="活动图片">
        <!-- 头衔缩略图 -->
        <pan-thumb :image="addData.pic" />
        <!-- 文件上传按钮 -->
        <el-button
          type="primary"
          icon="el-icon-upload"
          @click="imagecropperShow = true"
          >上传图片
        </el-button>
        <!--v-show：是否显示上传组件
:key：类似于id，如果一个页面多个图片上传控件，可以做区分
:url：后台上传的url地址
@close：关闭上传组件
@crop-upload-success：上传成功后的回调 -->
        <image-cropper
          v-show="imagecropperShow"
          :width="70"
          :height="70"
          :key="imagecropperKey"
          :url="BASE_API + '/oss/fileUpload'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"
        />
      </el-form-item>

    <el-form-item label="活动状态">
        <el-select  filterable placeholder="请选择" v-model="addData.activityState">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="报名费">
        <el-input v-model="addData.activityMoney" placeholder=""  style="width:200px">
        <template slot="prepend">¥：</template>
        </el-input>
      </el-form-item>

      <el-form-item label="赠送积分">
        <el-input v-model="addData.point" placeholder="参加活动时赠送的积分"  style="width:200px"/>  
        
      </el-form-item>

      <el-form-item label="名额">
        <el-input v-model="addData.places" placeholder="" style="width:200px">
        <template slot="append">人</template>
        </el-input>
      </el-form-item>

      <el-form-item label="是否推荐到热门">
        <el-select  filterable placeholder="否" v-model="addData.hot">
          <el-option
            v-for="item in hotOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="类容详情：">
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
          v-model="addData.decreption"  style="width:400px">
        </el-input>
      </el-form-item>

      <el-form-item label="排序号：">
        <el-input-number
          v-model="addData.sortId"
          controls-position="right"
          :min="0"
        />请填写排序号,数字越小越靠前
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate()"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import activityShow from "@/api/activityShow";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";

export default {
  components: { PanThumb, ImageCropper },
  data() {
    return {   
      options: [ {
          value: '正在进行中',
          label: '正在进行中'
        },{
          value: '即将开始',
          label: '即将开始'
        }],
        hotOptions: [ {
          value: '1',
          label: '是'
        }, {
          value: '0',
          label: '否'
        }], 
      addData: {
        id:"",
        name: "",
        pic: "",
        activityState:"",
        activityMoney:"",
        emplId:"",
        point:"",
        places:"",
        hot:"",
        decreption:"",
        sortId:"",
      },
      saveBtnDisabled: false, // 保存按钮是否禁用,
      imagecropperShow: false, //头像上传组件是否显示
      imagecropperKey: 0,
      BASE_API: process.env.BASE_API, //获取dev.env.js里面的地址
    };
  },
  created() {
    //页面渲染之前执行
    this.init();
  },
  watch: {
    //监听
    $route(to, from) {
      //路由变化方式，路由发生变化，方法就会执行
      this.init();
    },
  },
  methods: {
    close() {
      //关闭上传头像组件
      this.imagecropperShow = false;
      //上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    cropSuccess(data) {
      //上传成功后返回的图片地址
      this.addData.pic = data.fileUrl;
      //上传头像成功
      this.imagecropperShow = false;
      //上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    init() {
      //判断路径有id值,做修改
      if (this.$route.params && this.$route.params.id) {
        //从路径获取id值
        const id = this.$route.params.id;
        //调用根据id查询的方法
        this.getInfo(id);
      } else {
        //路径没有id值，做添加
        //清空表单
        this.slideShow = {};
      }
    },
    //根据讲师id查询的方法
    getInfo(id) {
      activityShow.getCurrentActivity(id).then((response) => {
        console.log(response.object) 
        this.addData = response.object;
      });
    },
    saveOrUpdate() {
      //判断修改还是添加
      //根据teacher是否有id
      if (!this.addData.id) {
        //添加
        this.addActivity();
      } else {
        //修改
        this.updateActivity();
      }
    },
    //修改轮播图的方法
    updateActivity() {
      activityShow.updateActivityShow(this.addData).then((response) => {
        //提示信息
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //回到列表页面 路由跳转
        this.$router.push("/list/activity");
      });
    },
    //添加轮播图的方法
    addActivity() {
      console.log(activityShow)
      activityShow.saveAddActivity(this.addData).then((response) => {
        //添加成功
        //提示信息
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //回到列表页面 路由跳转
        this.$router.push( "/list/activity" );
      });
    },
  },
};
</script>
